<template>
  <el-menu text-color="#fff" background-color="#545C64" :collapse="isCollapse" :collapse-transition="false" :default-active="activeMenu">
    <h3 class="title" v-show="!isCollapse">通用后台管理</h3>
    <h3 class="title" v-show="isCollapse">后台</h3>
    <el-menu-item
      v-for="item in noChildren"
      :key="item.path"
      :index="item.path"
      @click="handleSelect(item)"
    >
      <component :is="item.icon" class="icons" />
      <span>{{ item.label }}</span>
    </el-menu-item>

    <el-sub-menu
      v-for="item in hasChildren"
      :key="item.path"
      :index="item.path"
    >
      <template #title>
        <component class="icons" :is="item.icon" />
        <span>{{ item.label }}</span>
      </template>
      <el-menu-item-group>
        <el-menu-item
          v-for="(subItem, subindex) in item.children"
          :key="subItem.path"
          :index="subItem.path"
          @click="handleSelect(item)"
        >
          <component :is="subItem.icon" class="icons" />
          <span>{{ subItem.label }}</span>
        </el-menu-item>
      </el-menu-item-group>
    </el-sub-menu>
  </el-menu>
</template>
  
<script lang="ts" setup>
import { computed, ref } from "vue";
import { useAllDataStore } from "@/stores";
import { useRouter, useRoute } from "vue-router";
const router = useRouter();
const route = useRoute();
const activeMenu = computed(() => route.path || '/home');
interface MenuItem {
  path: string;
  name: string;
  label: string;
  icon: string;
  url: string;
  children?: MenuItem[];
}

const allDataStore = useAllDataStore();
const isCollapse = computed(() => allDataStore.state.isCollapsible);
const list = computed(()=> allDataStore.state.menuList);
const noChildren = computed(() => list.value.filter((item) => !item.children));
const hasChildren = computed(() => list.value.filter((item) => item.children));
const handleSelect = (index : string) => {
  if (index && index !== route.path) {
    allDataStore.selectMenu(index);
    router.push(index);
  }
}
const handleOpen = (key: string, keyPath: string[]) => {
  console.log(key, keyPath);
};
const handleClose = (key: string, keyPath: string[]) => {
  console.log(key, keyPath);
};
</script>
<style scoped>
.el-menu {
  height: 100%;
  border-right: none;
}
.icons {
  width: 18px;
  height: 18px;
  margin-right: 5px;
}
.title {
  color: #fff;
  text-align: center;
  line-height: 60px;
  font-size: 18px;
}
</style>